package com.haofengsoft.scce.adapter.index;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.ui.consultation.ConsultationDetailActivity;

import java.util.List;

/**
 * Created by admin on 2015/7/24.
 */
public class TopNewAdapter extends BaseAdapter {

    private Context context;
    private List<TopNewModel> topNewModel;
    private LayoutInflater inflater = null;

    public TopNewAdapter(Context context, List<TopNewModel> topNewModel) {
        this.context = context;
        this.topNewModel = topNewModel;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return this.topNewModel.size();
    }

    @Override
    public Object getItem(int position) {
        return this.topNewModel.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    public void addFocusNewList(List<TopNewModel> topNewModel) {
        this.topNewModel.addAll(topNewModel);
        // notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.index_top_new_list_item, null);
        ImageView typeTv = (ImageView) view.findViewById(R.id.index_top_new_source_type_iv);
        TextView contentTv = (TextView) view.findViewById(R.id.index_top_new_content_tv);
        TopNewModel model = (TopNewModel) getItem(position);
        typeTv.setImageResource(model.getModelTypeRes().getModelType());
        contentTv.setText(model.getContent());
        view.setOnClickListener(new ItemClickListener(model));
        return view;
    }

    class ItemClickListener implements View.OnClickListener {

        TopNewModel model;

        public ItemClickListener(TopNewModel TopNewModel) {
            this.model = TopNewModel;
        }

        @Override
        public void onClick(View view) {
            Intent detailIntent = new Intent(context, ConsultationDetailActivity.class);
            String sourceUrl = ConstantConfig.API_URL + ConstantConfig.INFO_CONNECT_WEB_URL + model.getId();
            detailIntent.putExtra("sourceUrl", sourceUrl);
            detailIntent.putExtra("title", model.getContent());
            detailIntent.putExtra("time", model.getDateTime());
            detailIntent.putExtra("source", model.getSource());
            detailIntent.putExtra("newsId",String.valueOf(model.getId()));
            detailIntent.putExtra("general", model.getSummary());
            context.startActivity(detailIntent);
        }

    }

    static class ViewHolder {
        ImageView sourceTv;
        TextView contentTv;
    }
}
